import { http } from '@/request/http'
import type { PageDTO, PageVO } from '@/type/type'
import type { LabelVO } from '@/request/api/label'
import { initLabelVO } from '@/request/api/label'
import type { MakeOptional } from '@/type'

type BaseWebsite = {
  // 主键
  id: number
  // 网站标题
  title: string
  // 头像
  avatar: string
  // 网站简介
  introduction: string
  // 链接
  url: string
  // 封面
  cover: string
  // 1推荐，0不推荐
  isRecommend: boolean
  // 是否启用
  status: boolean
}

export type WebsiteDTO = MakeOptional<BaseWebsite> & {
  // 用户ID
  userId: number | undefined
  // 标签ID
  labelId: number | undefined
}

export type WebsiteVO = BaseWebsite & {
  // 主键
  id: number
  // 用户ID
  userId: number
  // 标签
  label: LabelVO
  // 创建时间
  createTime: Date
}

export const initWebsiteDTO = (): WebsiteDTO => {
  return {
    id: undefined,
    userId: undefined,
    labelId: undefined,
    title: undefined,
    avatar: undefined,
    introduction: undefined,
    url: undefined,
    cover: undefined,
    isRecommend: undefined,
    status: undefined
  }
}

export const initWebsiteVO = (): WebsiteVO => {
  return {
    id: 0,
    userId: 0,
    label: initLabelVO(),
    title: '',
    avatar: '',
    introduction: '',
    url: '',
    cover: '',
    createTime: new Date(),
    isRecommend: false,
    status: true
  }
}

export const websiteVO2DTO = (vo: WebsiteVO): WebsiteDTO => {
  return {
    id: vo.id,
    userId: vo.userId,
    labelId: vo.label.id,
    title: vo.title,
    avatar: vo.avatar,
    introduction: vo.introduction,
    url: vo.url,
    cover: vo.cover,
    isRecommend: vo.isRecommend,
    status: vo.status
  }
}

export const getWebsitePageApi = (page: PageDTO) => {
  return http<PageVO<WebsiteVO>>({
    url: `/website/page`,
    method: 'get',
    params: {
      dto: JSON.stringify(page)
    }
  })
}

export const getWebsiteAdminPageApi = (page: PageDTO) => {
  return http<PageVO<WebsiteVO>>({
    url: `/admin/website/page`,
    method: 'get',
    params: {
      dto: JSON.stringify(page)
    }
  })
}

export const addWebsiteApi = (data: WebsiteDTO) => {
  return http<boolean>({
    url: `/website/insert`,
    method: 'post',
    data: data
  })
}

export const updateWebsiteApi = (data: WebsiteDTO) => {
  return http<boolean>({
    url: `/website/update`,
    method: 'put',
    data: data
  })
}

export const delWebsiteApi = (ids: number[]) => {
  return http<boolean>({
    url: `/website/delete`,
    method: 'delete',
    data: ids
  })
}
